var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teamList: [],
    isHidebtn: false,
    // 加载时第一次
    isonLoad: true,
    name: "",
    pageNo: 1,
    pageSize: 10,
    // 成功遮罩层
    issuccessMask: false,
    // 0店长 1岩板砖家
    typeindex:'0',
    // 特殊处理分享
    isOverShare:true,
    ispaidan:false,
    isworkdetail:false,
    workdetail:{}
  },


  hideisworkdetail(){
this.setData({
  isworkdetail:false
})
  },
  showdetail(e){
let teamList=this.data.teamList
 let workdetail=  teamList[e.currentTarget.dataset.index]
 this.setData({
  workdetail:workdetail,
  isworkdetail:true
 })
  },


  gopersonal(e){
    if(e.currentTarget.dataset.id==''){
      this.setData({
        ispaidan:true
      })
      return
    }
    wx.navigateTo({
      url: '/pages/my/personal/personal?userid='+e.currentTarget.dataset.id,
    })

  },



  // 搜索
  bindconfirm() {
    this.setData({
      pageNo: 1,
      pageSize: 10
    })
   if(this.data.identity==4){
     this.setData({
      typeindex:''
     })
    this.getBricklayList()
   }else if(this.data.identity==1){
    this.getSalesmanTeamPage()
   }else if(this.data.identity==2){
    this.getAgentTeamPage()
   }else if(this.data.identity==3){
    if(this.data.typeindex==0){
      this.getShopownerTeamPage()
    }else{
      this.getSuperviseTeamPage()
    }
   }
  },
  // 删除业务员
  delSalesman(salesmanId, index) {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'delSalesman',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {

        userId: app.globalData.userId,
        token: t.data.token,
        salesmanId: salesmanId
      },
      success: res => {
        wx.hideLoading()

        if (res.data.result == 0) {

          let teamList = t.data.teamList
          teamList.splice(index, 1)
          t.setData({
            teamList: teamList,
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },
   // 删除代理商
   delAgent(agentId, index) {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'delAgent',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {

        userId: app.globalData.userId,
        token: t.data.token,
        agentId: agentId
      },
      success: res => {
        wx.hideLoading()

        if (res.data.result == 0) {

          let teamList = t.data.teamList
          teamList.splice(index, 1)
          t.setData({
            teamList: teamList,
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  // 删除店长
  delShopowner(shopownerId, index) {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'delShopowner',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {

        userId: app.globalData.userId,
        token: t.data.token,
        shopownerId: shopownerId
      },
      success: res => {
        wx.hideLoading()

        if (res.data.result == 0) {

          let teamList = t.data.teamList
          teamList.splice(index, 1)
          t.setData({
            teamList: teamList,
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
    // 删除岩板砖家
    delSupervise(superviseId, index) {
      let t = this
      console.log('superviseId',superviseId)
      wx.showLoading({
        title: '加载中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
  
      wx.request({
        url: app.globalData.Interfaceurl + 'delSupervise',
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
  
          userId: app.globalData.userId,
          token: t.data.token,
          superviseId: superviseId
        },
        success: res => {
          wx.hideLoading()
  
          if (res.data.result == 0) {
  
            let teamList = t.data.teamList
            teamList.splice(index, 1)
            t.setData({
              teamList: teamList,
              issuccessMask: true
            })
          } else {
            wx.showToast({
              title: res.data.message,
              duration: 2500, //持续的时间
            icon: 'none', //图标
            })
            t.setData({
              token:app.token()
            })
          }
        },
        fail: err => {
          wx.showToast({
            title: '请求失败',
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
        }
      })
    },
// 删除成员
  del(e) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let t = this
    wx.showModal({
      title: '提示',
      content: '是否确认删除该成员？',
      confirmColor: "#D6000F",
      success(res) {
        if (res.confirm) {

       if(t.data.identity==1){
        t.delSalesman(id, index)
       }else if(t.data.identity==2){
        t.delAgent(id, index)
       }else if(t.data.identity==3){
       if(t.data.typeindex==0){
        t.delShopowner(id, index)
       }else if(t.data.typeindex==1){
        t.delSupervise(id, index)
       }
     
       }else if(t.data.identity==5||t.data.identity==10){
        if(t.data.typeindex==0){
         t.delDesigner(id, index)
        }else if(t.data.typeindex==1){
         t.delAdviser(id, index)
        }
      
        }


        } 
        
        
        else if (res.cancel) {

        }
      }
    })
  },
  delDesigner(designerId, index){
    let t = this
    console.log('designerId',designerId)
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'delDesigner',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {

        userId: app.globalData.userId,
        token: t.data.token,
        designerId: designerId
      },
      success: res => {
        wx.hideLoading()

        if (res.data.result == 0) {

          let teamList = t.data.teamList
          teamList.splice(index, 1)
          t.setData({
            teamList: teamList,
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  delAdviser(adviserId, index){
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'delAdviser',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {

        userId: app.globalData.userId,
        token: t.data.token,
        adviserId: adviserId
      },
      success: res => {
        wx.hideLoading()

        if (res.data.result == 0) {
          let teamList = t.data.teamList
          teamList.splice(index, 1)
          t.setData({
            teamList: teamList,
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },
  // 隐藏遮罩层
  HidesuccessMask() {
    this.setData({
      issuccessMask: false
    })
  },

  // 25.瓦工列表
  getBricklayList() {
    let t=this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'getBricklayList',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        name: t.data.name,
        workType:t.data.typeindex
      },
      success: res => {
        wx.hideLoading();
        res.data.bricklayList.map(item => item.isshow = false)
        res.data.bricklayList.map(item => item.customerGrade = Math.ceil(item.customerGrade))
        t.setData({
          teamList: res.data.bricklayList
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },
  // 业务员列表
  getSalesmanTeamPage() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'getSalesmanTeamPage',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        pageNo: t.data.pageNo,
        pageSize: t.data.pageSize,
        name:t.data.name
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        let teamList = t.data.teamList
        res.data.page.list.map(item => item.isshow = false)
        if (t.data.pageNo == 1) {
          teamList = res.data.page.list
        } else {
          teamList.push(...res.data.page.list)
        }
        t.setData({
          teamList: teamList,
          last: res.data.page.last
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },
  // 代理商页表
  getAgentTeamPage() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'getAgentTeamPage',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        pageNo: t.data.pageNo,
        pageSize: t.data.pageSize,
        name:t.data.name
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        let teamList = t.data.teamList
        res.data.page.list.map(item => item.isshow = false)
        if (t.data.pageNo == 1) {
          teamList = res.data.page.list
        } else {
          teamList.push(...res.data.page.list)
        }
        t.setData({
          teamList: teamList,
          last: res.data.page.last
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },

 // 店长页表
 getShopownerTeamPage() {
  let t = this
  wx.showLoading({
    title: '加载中...', //提示的内容
    mask: true //显示透明蒙层 防止触摸穿透
  })

  wx.request({
    url: app.globalData.Interfaceurl + 'getShopownerTeamPage',
    method: "post",
    header: {
      "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    },
    data: {
      userId: app.globalData.userId,
      pageNo: t.data.pageNo,
      pageSize: t.data.pageSize,
      name:t.data.name
    },
    success: res => {
      console.log(res)
      wx.hideLoading()
      let teamList = t.data.teamList
      res.data.page.list.map(item => item.isshow = false)
      if (t.data.pageNo == 1) {
        teamList = res.data.page.list
      } else {
        teamList.push(...res.data.page.list)
      }
      t.setData({
        teamList: teamList,
        last: res.data.page.last
      })
    },
    fail: err => {
      wx.showToast({
        title: '请求失败',
        duration: 2500, //持续的时间
            icon: 'none', //图标
      })
    }
  })
},
// 岩板砖家列表
getSuperviseTeamPage(){
  let t = this
  wx.showLoading({
    title: '加载中...', //提示的内容
    mask: true //显示透明蒙层 防止触摸穿透
  })

  wx.request({
    url: app.globalData.Interfaceurl + 'getSuperviseTeamPage',
    method: "post",
    header: {
      "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    },
    data: {
      userId: app.globalData.userId,
      pageNo: t.data.pageNo,
      pageSize: t.data.pageSize,
      name:t.data.name
    },
    success: res => {

      wx.hideLoading()
      let teamList = t.data.teamList
      res.data.page.list.map(item => item.isshow = false)
      if (t.data.pageNo == 1) {
        teamList = res.data.page.list
      } else {
        teamList.push(...res.data.page.list)
      }
      t.setData({
        teamList: teamList,
        last: res.data.page.last
      })
    },
    fail: err => {
      wx.showToast({
        title: '请求失败',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
    }
  })
},
  // 获得设计师列表
  getDesignerList() {
    let t = this
    wx.showLoading({
      title: '加载中',
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getDesignerList',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        name: t.data.name,
      },
      success: res => {
        wx.hideLoading()
  

        let designerList = res.data.designerList
     if(designerList.length!=0){
      designerList.forEach(item => {
        item.isshow = false
      });
     }
        t.setData({
          teamList: designerList
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },
// 获得家居顾问列表
getAdviserList(){
  let t = this
  wx.showLoading({
    title: '加载中',
    
    mask: true //显示透明蒙层 防止触摸穿透
  })
  wx.request({
    url: app.globalData.Interfaceurl + 'getAdviserList',
    method: "post",
    header: {
      "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    },
    data: {
      userId: app.globalData.userId,
      name: t.data.name,
    },
    success: res => {
      wx.hideLoading()


      let adviserList = res.data.adviserList
     if(adviserList.length!=0){
      adviserList.forEach(item => {
        item.isshow = false
      });
     }
      t.setData({
        teamList: adviserList
      })
    },
    fail: err => {
      wx.showToast({
        title: '请求失败',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
    }
  })
},

  // 联系
  contact(e) {
    let mobile = e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile //仅为示例，并非真实的电话号码
    })
  },
  // 编辑
  editor(e) {
  if(this.data.identity==4){
    wx.navigateTo({
      url: '/supervisor/pages/addpeople/addpeople?id=' + e.currentTarget.dataset.id,
    })
  }else if(this.data.identity==1){
    wx.navigateTo({
      url: '/pages/addpeople/addpeople?todo=addsalesman&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
    })
  }else if(this.data.identity==2){
    wx.navigateTo({
      url: '/pages/addpeople/addpeople?todo=addagent&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
    })
  }
  else if(this.data.identity==3){
if(this.data.typeindex==0){
  wx.navigateTo({
    url: '/pages/addpeople/addpeople?todo=addshopowner&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
  })
}else{
  wx.navigateTo({
    url: '/pages/addpeople/addpeople?todo=addsupervisor&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
  })
}
  }else if(this.data.identity==5){
    if(this.data.typeindex==0){
      wx.navigateTo({
        url: '/pages/addpeople/addpeople?todo=adddesigner&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
      })
    }else{
      wx.navigateTo({
        url: '/pages/addpeople/addpeople?todo=addstaff&&detail='+JSON.stringify(e.currentTarget.dataset.detail),
      })
    }
  } 
  },
  // 显示按钮
  showBtn(e) {
    let index = e.currentTarget.dataset.index
    let teamList = this.data.teamList
    teamList.forEach((item1, index1) => {
      if (index1 == index) {
        item1.isshow = !item1.isshow
      } else {
        item1.isshow = false
      }
    })

    this.setData({
      teamList: teamList
    })

  },


  // 添加人员
  add() {
    if (this.data.identity == 4) {
      wx.navigateTo({
        url: '/supervisor/pages/addpeople/addpeople?typeindex='+(this.data.typeindex-1),
      })
    } else if (this.data.identity == 1) {
      wx.navigateTo({
        url: '/pages/addpeople/addpeople?todo=addsalesman',
      })
    }else if(this.data.identity == 2){
      wx.navigateTo({
        url: '/pages/addpeople/addpeople?todo=addagent',
      })
    }else if(this.data.identity == 3){
      if(this.data.typeindex==0){
        wx.navigateTo({
          url: '/pages/addpeople/addpeople?todo=addshopowner',
        })
      }else{
        wx.navigateTo({
          url: '/pages/addpeople/addpeople?todo=addsupervisor',
        })
      }
    }else if(this.data.identity == 5||this.data.identity == 10){
      if(this.data.typeindex==0){
        wx.navigateTo({
          url: '/pages/addpeople/addpeople?todo=adddesigner',
        })
      }else{
        wx.navigateTo({
          url: '/pages/addpeople/addpeople?todo=addstaff',
        })
      }
    }
  },
  // 隐藏授权
  hidepaidan(){
    this.setData({
      ispaidan:false
    })
  },
  // 派单 
  binddispatch(e) {




    let Bricklayer = e.currentTarget.dataset.item


    if (Bricklayer.verifyFlag == 0) {

      return
    }


    // if (Bricklayer.userId == '' && Bricklayer.verifyFlag != 0) {
    //   this.setData({
    //     ispaidan:true
    //   })
    //   // wx.showToast({
    //   //   title: '该瓦工还没有授权,请联系瓦工授权再派单',
    //   //   icon: 'none'

    //   // })
    //   return
    // }
    wx.navigateTo({

      url: '/supervisor/pages/beforconstruction/beforconstruction?Bricklayer=' + JSON.stringify(Bricklayer)
    })
  },
  bindtype(e){
    this.setData({
      typeindex:e.currentTarget.dataset.typeindex,
      teamList:[]
    })
    this.setData({
      pageNo: 1,
      pageSize: 10
    })
  if(this.data.identity==3){
    if(this.data.typeindex==0){
      this.getShopownerTeamPage()
    }else{
      this.getSuperviseTeamPage()
    }
  }else if(this.data.identity==5){
    if(this.data.typeindex==0){
      this.getDesignerList()
    }else{
      this.getAdviserList()
    }
  }else if(this.data.identity==4){
   this.getBricklayList()
  }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let myTab = this.selectComponent('#myTab'); // 页面获取自定义组件实例
    myTab.gettabList(); // 通过实例调用组件事件
 


    this.setData({
      // 自适应
      value: app.globalData.width * 2 * 0.121,
      identity: app.globalData.identity
    })
    if (app.globalData.identity == 4) {
      this.setData({
        typeindex:1
      })
      this.getBricklayList()
    } else if (app.globalData.identity == 1) {
      this.getSalesmanTeamPage()
    }else if (app.globalData.identity == 2) {
      this.getAgentTeamPage()
    }else if (app.globalData.identity == 3) {
      this.getShopownerTeamPage()
    }else if (app.globalData.identity == 5||app.globalData.identity == 10) {
      this.getDesignerList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myTab = this.selectComponent('#myTab'); // 页面获取自定义组件实例
        myTab.gettabList(); // 通过实例调用组件事件
    this.setData({
      token: app.token(),
      ispaidan:false,
      identity:app.globalData.identity
    })
    if (!this.data.isonLoad) {
      if (wx.getStorageSync('istab') == true) {
        
        this.setData({
          pageNo: 1,
          pageSize: 10
        })
        if (app.globalData.identity == 4) {
          this.getBricklayList()
        } else if (app.globalData.identity == 1) {

          this.getSalesmanTeamPage()
        }else if(app.globalData.identity == 2){
          this.getAgentTeamPage()
        }else if(app.globalData.identity == 3){
          if(this.data.typeindex==0){
            this.getShopownerTeamPage()
          }else{
            this.getSuperviseTeamPage()
          }
        }else if(app.globalData.identity == 5||app.globalData.identity == 10){
          if(this.data.typeindex==0){
            this.getDesignerList()
          }else{
            this.getAdviserList()
          }
        }



       


      }
    } else {
      this.setData({
        isonLoad: false
      })
  
    }
    wx.setStorageSync('istab', false)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (app.globalData.identity == 1) {
      if (this.data.pageNo >= this.data.last) {
        return
      }
      this.setData({
        pageNo: this.data.pageNo + 1
      })
      this.getSalesmanTeamPage()
    }else if(app.globalData.identity == 2){
      if (this.data.pageNo >= this.data.last) {
        return
      }
      this.setData({
        pageNo: this.data.pageNo + 1
      })
      this.getAgentTeamPage()
    }else if(app.globalData.identity == 2){
      if (this.data.pageNo >= this.data.last) {
        return
      }
      this.setData({
        pageNo: this.data.pageNo + 1
      })
      if(this.data.typeindex==0){
        this.getShopownerTeamPage()
      }else{
        this.getSuperviseTeamPage()
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
  
    if(app.globalData.identity==4){
      return {
        title: '您有一个新订单！顺辉包铺贴，技能上岗，收入翻倍！',
        path: "/pages/index/index", //若无path 默认跳转分享页
        imageUrl: 'https://shunhui.at1984.com:8443/resource/userfiles/shunhui/shunhui_share.png',//若无imageUrl 截图当前页面
       }
    }else{
      return {
        title: '顺辉邀您一起全民监工，真实案例专业服务让您一生顺心！',
        path: "/pages/index/index", //若无path 默认跳转分享页
        imageUrl: 'https://shunhui.at1984.com:8443/resource/userfiles/shunhui/shunhui_share.png',//若无imageUrl 截图当前页面
       }
    }
  }
})